package com.yx.system.controller;

import java.util.List;
import java.util.Map;

import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.system.entity.SysAuthBookInfo;
import com.yx.system.entity.SysUser;
import com.yx.system.service.SysAuthBookInfoService;
import com.yx.modules.api.service.SysAuthBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 第三方图书详情
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-10 15:37:29
 */
@RestController
@RequestMapping("/sys/authbookinfo")
public class SysAuthBookInfoController {
	
	@Autowired
	private SysAuthBookInfoService authBookInfoService;

	@Autowired
	private SysAuthBookService sysAuthBookService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:authbookinfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysAuthBookInfo> authBookInfoList = authBookInfoService.queryList(query);
		int total = authBookInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(authBookInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:authbookinfo:info")
	public R info(@PathVariable("id") String id){
		SysAuthBookInfo authBookInfo = authBookInfoService.queryObject(id);
		
		return R.ok().put("authBookInfo", authBookInfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:authbookinfo:save")
	public R save(@RequestBody SysAuthBookInfo authBookInfo){
		authBookInfoService.save(authBookInfo);
		
		return R.ok();
	}

	/*
	* 导入第三方图书
	*/
	@RequestMapping("put")
    @RequiresPermissions("sys:authbookinfo:put")
	public R put(@RequestBody SysAuthBookInfo authBookInfo, @LoginUser2 SysUser sysUser){
        Assert.isNull(authBookInfo.getCid(),"请选择图书分类");
		Thread thread = new Thread(){
			public void run(){
				 authBookInfoService.put(authBookInfo,sysUser);
			}
		};
		thread.start();
		authBookInfo.setStatus(2);
		authBookInfoService.update(authBookInfo);
        return R.ok("图书导入需要时间，请在几分钟后刷新页面查看导入状态");
    }


	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:authbookinfo:update")
	public R update(@RequestBody SysAuthBookInfo authBookInfo){
		authBookInfoService.update(authBookInfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:authbookinfo:delete")
	public R delete(@RequestBody String[] ids){
		Thread thread = new Thread(){
			public void run(){
				authBookInfoService.deleteBatch(ids);
			}
		};
		thread.start();
		return R.ok("正在清理数据，请在几分钟后刷新查看");

	}
	
}
